package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.ApplyChuchaiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.AreaBean;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.EditBean;
import com.eqdd.yiqidian.bean.StaffBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.ui.base.BaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectStartEndTimeActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.MultiChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.ISelectAble;
import rx.Observable;

public class ApplyChuchaiActivity extends BaseMultiEditActivity {
    private ApplyChuchaiActivityCustom dataBinding;

    private String[] titles = new String[]{
            "企业",
            "部门",
            "工号",
            "姓名",
            "职位",
            "出差时间",
            "出差地点",
            "详细地址",
            "审核人"
    };
    private String[] hintTexts = new String[]{
            "",
            "",
            "",
            "",
            "",
            "请选择",
            "请选择",
            "请选择",
            ""
    };
    private int[] types = new int[]{
            4, 4, 4, 4, 4, 4, 4, 4, 4
    };

    private static final int TIME = 5;
    private static final int AREA = 6;
    private static final int AREA_DEATAIL = 7;

    private Bundle bundle;
    private String startTime;
    private String endTime;
    private List<AreaBean> areaBean;
    private BottomDialog areaDialog;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_apply_chuchai);
        initTopTitleBar(View.VISIBLE, "出差申请");
        initTopRightText("提交", v -> {

        });
    }

    @Override
    public void initData() {
        new HttpPresneter.Builder<String>(HttpConfig.SELECT_STAFF, String.class, this)
                .Params("uid", user.getUname())
                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {
                        BaseBean<StaffBean> baseBean = GsonUtils.changeGsonToBaseBean((String) o, StaffBean.class);
                        if (baseBean.getStatus() == 200) {
                            if (baseBean.getItems() != null && baseBean.getItems().size() > 0 && !baseBean.getItems().get(0).getCid().equals("0")) {
                                if (user.getAuthen() == 1) {
                                    StaffBean staffBean = baseBean.getItems().get(0);
                                    for (int i = 0; i < titles.length; i++) {
                                        EditBean editBean = new EditBean();
                                        editBean.setTitle(titles[i]);
                                        editBean.setType(types[i]);
                                        editBean.setContent("");
                                        editBean.setTextTitle(titles[i]);
                                        switch (i) {
                                            case 0://企业
                                                editBean.setEnter(false);
                                                editBean.setContent(staffBean.getCname());
                                                break;
                                            case 1://部门
                                                editBean.setEnter(false);
                                                editBean.setContent(staffBean.getPostname());
                                                break;
                                            case 2://工号
                                                editBean.setEnter(false);
                                                editBean.setContent(user.getUname());
                                                break;
                                            case 3://名字
                                                editBean.setEnter(false);
                                                editBean.setContent(user.getName());
                                                break;
                                            case 4://职位
                                                editBean.setEnter(false);
                                                editBean.setContent(staffBean.getCareername());
                                                break;
                                        }
                                        editBeans.add(editBean);
                                    }
                                    if (quickAdapter != null) {
                                        ArrayList<EditBean> temp = new ArrayList<>();
                                        temp.addAll(editBeans);
                                        quickAdapter.notifyDataChanged(temp, true);
                                    }

                                } else {
                                    ToastUtil.showShort("请先进行实名认证");
                                    finish();
                                }
                            } else {
                                ToastUtil.showShort("你还没有入住公司呢");
                                finish();
                            }
                        } else {
                            finish();
                        }
                    }
                })
                .build()
                .post();
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case TIME:
                bundle = new Bundle();
                bundle.putInt(Config.TIME_TYPE, Config.YEAR_MONTH_DAY);
                startActivityForResult(SelectStartEndTimeActivity.class, bundle, Config.SELECT_START_END_TIME);
                break;
            case AREA:
                if (areaDialog==null){
                    initAreaDialog();
                }else {
                    areaDialog.show();
                }
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_START_END_TIME:
                    startTime = data.getStringExtra(Config.START_TIME);
                    endTime = data.getStringExtra(Config.END_TIME);
                    quickAdapter.getData().get(TIME).setContent("已选择");
                    quickAdapter.notifyItemChanged(TIME);
                    break;
            }

        }

    }

    private void initAreaDialog() {
        Observable.just(1)
                .flatMap((integer) ->
                        new HttpPresneter.Builder<String>(HttpConfig.AREA_OPTIONS, String.class, null)
                                .CacheTime(60 * 1000 * 24 + "")
                                .build()
                                .getObserverBean())
                .flatMap((integer) -> {
                            String msg = null;
                            try {
                                msg = new JSONObject((String) integer).getString("msg");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String url = HttpConfig.BASE_URL_NO + msg;
                            return new HttpPresneter.Builder<String>(url, String.class, null)
                                    .CacheTime(60 * 1000 * 24 + "")
                                    .build()
                                    .getObserverBean();
                        }
                )
                .subscribe((s) -> {
                    areaBean = new Gson().fromJson((String) s, new TypeToken<List<AreaBean>>() {
                    }.getType());
                    areaDialog = new MultiChooseUtil().getAreaDialog(ApplyChuchaiActivity.this, 3, areaBean, (d, a) -> {
                        areaDialog.dismiss();
                        String result = "";
                        for (ISelectAble iSelectAble : a) {
                            result += iSelectAble.getName();
                        }
                        editBeans.get(AREA).setContent(result);
                        quickAdapter.notifyItemChanged(AREA);

                    });
                    areaDialog.show();
                },(e)->{
                    areaDialog=null;
                    ToastUtil.showShort(Config.SERVER_ERROR);
                });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
